package cn.zclong.publish.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itcast.commons.CommonUtils;
import cn.zclong.article.domain.Article;
import cn.zclong.publish.service.PublishService;

/**
 * Servlet implementation class ForwardServlet
 */
public class ForwardServlet extends HttpServlet {
	private PublishService publishService = new PublishService();
	
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out= response.getWriter();
		try {
			/*
			 * 获取参数，转发人id，标题，内容，阅读量
			 * uid:发帖人的id，自己不能转发自己的帖子
			 */
			String userid = request.getParameter("userid");
			String title = request.getParameter("title");
			String message = request.getParameter("message");
			String readnum1 = request.getParameter("readnum");	
			System.out.println(userid);
			String uid = request.getParameter("uid");	
			//判断帖子的id和登录者的id是否相同
			if(uid.equals(userid)) {
				out.print(-1);//出错
				return ;
			}
			
			
			
			
			long readnum = Long.parseLong(readnum1);
			
			if(userid==null || title==null || message==null){
				out.print(0);//出错
				return ;
			}
			
			Article article = new Article();
			article.setId(CommonUtils.uuid());//设置文章的唯一标识id
			article.setMessage(message);//设置文章内容
			article.setReadnum(0);//设置文章初始访问量为0
			article.setTime(new Date().getTime());//设置文章的发表时间
			article.setTitle(title);//设置文章的标题
			article.setUid(userid);//设置文章的转发人
			
			String articleId = publishService.publish(article);//转发成功返回文章id
			if(articleId==null){
				out.print(0);
			}else{
				out.print(articleId);
			}
		} catch (Exception e) {
			out.print(0);
			e.printStackTrace();
		}
	}

}
